<!DOCTYPE HTML>
<html lang="en-US" manifest="../manifest.appcache">
    
    <head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
        
        
        <meta charset="UTF-8">
        <title>《The Swift Programming Language》中文版</title>

        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="robots" content="index, follow">
        <meta name="author" content="">
        <meta name="description" content="Swift 中文翻译组：364279588（要求对翻译感兴趣）">
        <meta name="keywords" content="gitbook,github" >
        <meta name="generator" content="www.gitbook.io">

        
        <link rel="next" href="../chapter3/03_Types.html" />
        
        
        <link rel="prev" href="../chapter3/01_About_the_Language_Reference.html" />
        

        <meta property="og:title" content="词法结构 | 这一次，让中国和世界同步">
        <meta property="og:site_name" content="这一次，让中国和世界同步">
        <meta property="og:type" content="book">
        <meta property="og:locale" content="en_US">

        <meta property="book:author" content="https://github.com/">
        <meta property="book:tag" content="GitBook">

        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">

        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
        
    </head>
    <body>
        
        

        <link rel="stylesheet" href="../gitbook/style.css">
        


        
    <div class="book"  data-level="3.2" data-basepath=".." data-revision="1402523087598">
    <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    
    <a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
    
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    <span id="font-settings-wrapper">
        <a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="btn-group btn-block">
        <button id="reduce-font-size" class="btn btn-default">A</button>
        <button id="enlarge-font-size" class="btn btn-default">A</button>
    </div>

    <ul class="list-group font-family-list">
        <li class="list-group-item" data-font="0">Serif</li>
        <li class="list-group-item" data-font="1">Sans</li>
    </ul>

    <div class="btn-group btn-group-xs btn-block color-theme-list">
        <button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
        <button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
    </div>
</div>

    </span>

    <!-- Actions Right -->
    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    

    

    <!-- Title -->
    <h1>
        <i class="fa fa-spinner fa-spin"></i>
        <a href="../" >这一次，让中国和世界同步</a>
    </h1>
</div>

    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Search" class="form-control" />
    </div>
    <ul class="summary">
        
        

        

        

        

        <li data-level="0" data-path="index.html">
            <a href="../"><i class="fa fa-check"></i> 序</a>
        </li>
        
    
        <li class="chapter " data-level="1" data-path="chapter1/chapter1.html">
            
            <a href="../chapter1/chapter1.html">
                <i class="fa fa-check"></i> <b>1.</b> 欢迎使用 Swift
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/01_swift.html">
            
            <a href="../chapter1/01_swift.html">
                <i class="fa fa-check"></i> <b>1.1.</b> 关于 Swift
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/02_a_swift_tour.html">
            
            <a href="../chapter1/02_a_swift_tour.html">
                <i class="fa fa-check"></i> <b>1.2.</b> Swift 初见
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2/chapter2.html">
            
            <a href="../chapter2/chapter2.html">
                <i class="fa fa-check"></i> <b>2.</b> Swift 教程
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/01_The_Basics.html">
            
            <a href="../chapter2/01_The_Basics.html">
                <i class="fa fa-check"></i> <b>2.1.</b> 基础部分
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
            
            <a href="../chapter2/02_Basic_Operators.html">
                <i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/03_Strings_and_Characters.html">
            
            <a href="../chapter2/03_Strings_and_Characters.html">
                <i class="fa fa-check"></i> <b>2.3.</b> 字符串和字符
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="chapter2/04_Collection_Types.html">
            
            <a href="../chapter2/04_Collection_Types.html">
                <i class="fa fa-check"></i> <b>2.4.</b> 集合类型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="chapter2/05_Control_Flow.html">
            
            <a href="../chapter2/05_Control_Flow.html">
                <i class="fa fa-check"></i> <b>2.5.</b> 控制流
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="chapter2/06_Functions.html">
            
            <a href="../chapter2/06_Functions.html">
                <i class="fa fa-check"></i> <b>2.6.</b> 函数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="chapter2/07_Closures.html">
            
            <a href="../chapter2/07_Closures.html">
                <i class="fa fa-check"></i> <b>2.7.</b> 闭包
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="chapter2/08_Enumerations.html">
            
            <a href="../chapter2/08_Enumerations.html">
                <i class="fa fa-check"></i> <b>2.8.</b> 枚举
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="chapter2/09_Classes_and_Structures.html">
            
            <a href="../chapter2/09_Classes_and_Structures.html">
                <i class="fa fa-check"></i> <b>2.9.</b> 类和结构体
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="chapter2/10_Properties.html">
            
            <a href="../chapter2/10_Properties.html">
                <i class="fa fa-check"></i> <b>2.10.</b> 属性
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="chapter2/11_Methods.html">
            
            <a href="../chapter2/11_Methods.html">
                <i class="fa fa-check"></i> <b>2.11.</b> 方法
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
            
            <a href="../chapter2/12_Subscripts.html">
                <i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="chapter2/13_Inheritance.html">
            
            <a href="../chapter2/13_Inheritance.html">
                <i class="fa fa-check"></i> <b>2.13.</b> 继承
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
            
            <a href="../chapter2/14_Initialization.html">
                <i class="fa fa-check"></i> <b>2.14.</b> 构造过程
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
            
            <a href="../chapter2/15_Deinitialization.html">
                <i class="fa fa-check"></i> <b>2.15.</b> 析构过程
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.16" data-path="chapter2/16_Automatic_Reference_Counting.html">
            
            <a href="../chapter2/16_Automatic_Reference_Counting.html">
                <i class="fa fa-check"></i> <b>2.16.</b> 自动引用计数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.17" data-path="chapter2/17_Optional_Chaining.html">
            
            <a href="../chapter2/17_Optional_Chaining.html">
                <i class="fa fa-check"></i> <b>2.17.</b> 可选链
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.18" data-path="chapter2/18_Type_Casting.html">
            
            <a href="../chapter2/18_Type_Casting.html">
                <i class="fa fa-check"></i> <b>2.18.</b> 类型检查
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.19" data-path="chapter2/19_Nested_Types.html">
            
            <a href="../chapter2/19_Nested_Types.html">
                <i class="fa fa-check"></i> <b>2.19.</b> 类型嵌套
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.20" data-path="chapter2/20_Extensions.html">
            
            <a href="../chapter2/20_Extensions.html">
                <i class="fa fa-check"></i> <b>2.20.</b> 扩展
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.21" data-path="chapter2/21_Protocols.html">
            
            <a href="../chapter2/21_Protocols.html">
                <i class="fa fa-check"></i> <b>2.21.</b> 协议
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.22" data-path="chapter2/22_Generics.html">
            
            <a href="../chapter2/22_Generics.html">
                <i class="fa fa-check"></i> <b>2.22.</b> 泛型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.23" data-path="chapter2/23_Advanced_Operators.html">
            
            <a href="../chapter2/23_Advanced_Operators.html">
                <i class="fa fa-check"></i> <b>2.23.</b> 高级操作符
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3/chapter3.html">
            
            <a href="../chapter3/chapter3.html">
                <i class="fa fa-check"></i> <b>3.</b> 语言参考
            </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/01_About_the_Language_Reference.html">
            
            <a href="../chapter3/01_About_the_Language_Reference.html">
                <i class="fa fa-check"></i> <b>3.1.</b> 关于语言参考
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/02_Lexical_Structure.html">
            
            <a href="../chapter3/02_Lexical_Structure.html">
                <i class="fa fa-check"></i> <b>3.2.</b> 词法结构
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/03_Types.html">
            
            <a href="../chapter3/03_Types.html">
                <i class="fa fa-check"></i> <b>3.3.</b> 类型
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="chapter3/04_Expressions.html">
            
            <a href="../chapter3/04_Expressions.html">
                <i class="fa fa-check"></i> <b>3.4.</b> 表达式
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="chapter3/10_Statements.html">
            
            <a href="../chapter3/10_Statements.html">
                <i class="fa fa-check"></i> <b>3.5.</b> 语句
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="chapter3/05_Declarations.html">
            
            <a href="../chapter3/05_Declarations.html">
                <i class="fa fa-check"></i> <b>3.6.</b> 声明
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="chapter3/06_Attributes.html">
            
            <a href="../chapter3/06_Attributes.html">
                <i class="fa fa-check"></i> <b>3.7.</b> 特性
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="chapter3/07_Patterns.html">
            
            <a href="../chapter3/07_Patterns.html">
                <i class="fa fa-check"></i> <b>3.8.</b> 模式
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="chapter3/08_Generic_Parameters_and_Arguments.html">
            
            <a href="../chapter3/08_Generic_Parameters_and_Arguments.html">
                <i class="fa fa-check"></i> <b>3.9.</b> 泛型参数
            </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="chapter3/09_Summary_of_the_Grammar.html">
            
            <a href="../chapter3/09_Summary_of_the_Grammar.html">
                <i class="fa fa-check"></i> <b>3.10.</b> 语法总结
            </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
        </li>
<li style="margin-left:15%;"> <iframe  src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
  allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="page-wrapper" tabindex="-1">
                <div class="book-progress">
    <div class="bar">
        <div class="inner" style="width: 81.57894736842105%;min-width: 78.94736842105263%;"></div>
    </div>
    <div class="chapters">
    
        <a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
    
        <a href="../chapter1/chapter1.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 2.6315789473684212%;"></a>
    
        <a href="../chapter1/01_swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 5.2631578947368425%;"></a>
    
        <a href="../chapter1/02_a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 7.894736842105263%;"></a>
    
        <a href="../chapter2/chapter2.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 10.526315789473685%;"></a>
    
        <a href="../chapter2/01_The_Basics.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 13.157894736842104%;"></a>
    
        <a href="../chapter2/10_Properties.html" title="属性" class="chapter done " data-progress="2.10" style="left: 15.789473684210526%;"></a>
    
        <a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
    
        <a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
    
        <a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
    
        <a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 26.31578947368421%;"></a>
    
        <a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 28.94736842105263%;"></a>
    
        <a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 31.57894736842105%;"></a>
    
        <a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter done " data-progress="2.17" style="left: 34.21052631578947%;"></a>
    
        <a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter done " data-progress="2.18" style="left: 36.8421052631579%;"></a>
    
        <a href="../chapter2/19_Nested_Types.html" title="类型嵌套" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
    
        <a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
    
        <a href="../chapter2/20_Extensions.html" title="扩展" class="chapter done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
    
        <a href="../chapter2/21_Protocols.html" title="协议" class="chapter done " data-progress="2.21" style="left: 47.36842105263158%;"></a>
    
        <a href="../chapter2/22_Generics.html" title="泛型" class="chapter done " data-progress="2.22" style="left: 50%;"></a>
    
        <a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter done " data-progress="2.23" style="left: 52.63157894736842%;"></a>
    
        <a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter done " data-progress="2.3" style="left: 55.26315789473684%;"></a>
    
        <a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter done " data-progress="2.4" style="left: 57.89473684210526%;"></a>
    
        <a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter done " data-progress="2.5" style="left: 60.526315789473685%;"></a>
    
        <a href="../chapter2/06_Functions.html" title="函数" class="chapter done " data-progress="2.6" style="left: 63.1578947368421%;"></a>
    
        <a href="../chapter2/07_Closures.html" title="闭包" class="chapter done " data-progress="2.7" style="left: 65.78947368421052%;"></a>
    
        <a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter done " data-progress="2.8" style="left: 68.42105263157895%;"></a>
    
        <a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter done " data-progress="2.9" style="left: 71.05263157894737%;"></a>
    
        <a href="../chapter3/chapter3.html" title="语言参考" class="chapter done new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
    
        <a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter done " data-progress="3.1" style="left: 76.3157894736842%;"></a>
    
        <a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter done " data-progress="3.10" style="left: 78.94736842105263%;"></a>
    
        <a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter done " data-progress="3.2" style="left: 81.57894736842105%;"></a>
    
        <a href="../chapter3/03_Types.html" title="类型" class="chapter  " data-progress="3.3" style="left: 84.21052631578948%;"></a>
    
        <a href="../chapter3/04_Expressions.html" title="表达式" class="chapter  " data-progress="3.4" style="left: 86.84210526315789%;"></a>
    
        <a href="../chapter3/10_Statements.html" title="语句" class="chapter  " data-progress="3.5" style="left: 89.47368421052632%;"></a>
    
        <a href="../chapter3/05_Declarations.html" title="声明" class="chapter  " data-progress="3.6" style="left: 92.10526315789474%;"></a>
    
        <a href="../chapter3/06_Attributes.html" title="特性" class="chapter  " data-progress="3.7" style="left: 94.73684210526316%;"></a>
    
        <a href="../chapter3/07_Patterns.html" title="模式" class="chapter  " data-progress="3.8" style="left: 97.36842105263158%;"></a>
    
        <a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter  " data-progress="3.9" style="left: 100%;"></a>
    
    </div>
</div>

                <div class="page-inner">
                
                    <section class="normal" id="section-gitbook_6527">
                    
                        <blockquote>
<p>翻译：superkam</p>
<p>校对：numbbbbb</p>
</blockquote>
<h1 id="-">词法结构</h1>
<hr>
<p>本页包含内容：</p>
<ul>
<li><a href="#whitespace_and_comments">空白与注释（<em>Whitespace and Comments</em>）</a></li>
<li><a href="#identifiers">标识符（<em>Identifiers</em>）</a></li>
<li><a href="#keywords">关键字（<em>Keywords</em>）</a></li>
<li><a href="#literals">字面量（<em>Literals</em>）</a></li>
<li><a href="#operators">运算符（<em>Operators</em>）</a></li>
</ul>
<p>Swift 的“词法结构（<em>lexical structure</em>）”描述了如何在该语言中用字符序列构建合法标记，组成该语言中最底层的代码块，并在之后的章节中用于描述语言的其他部分。</p>
<p>通常，标记在随后介绍的语法约束下，由 Swift 源文件的输入文本中提取可能的最长子串生成。这种方法称为“最长匹配项（<em>longest match</em>）”，或者“最大适合”（<em>maximal munch</em>）。</p>
<p><a name="whitespace_and_comments"></a></p>
<h2 id="-">空白与注释</h2>
<p>空白（<em>whitespace</em>）有两个用途：分隔源文件中的标记和区分运算符属于前缀还是后缀，（参见 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_871" target="_blank">运算符</a>）在其他情况下则会被忽略。以下的字符会被当作空白：空格（<em>space</em>）（U+0020）、换行符（<em>line feed</em>）（U+000A）、回车符（<em>carriage return</em>）（U+000D）、水平 tab（<em>horizontal tab</em>）（U+0009）、垂直 tab（<em>vertical tab</em>）（U+000B）、换页符（<em>form feed</em>）（U+000C）以及空（<em>null</em>）（U+0000）。</p>
<p>注释（<em>comments</em>）被编译器当作空白处理。单行注释由 <code>//</code> 开始直到该行结束。多行注释由 <code>/*</code> 开始，以 <code>*/</code> 结束。可以嵌套注释，但注意注释标记必须匹配。</p>
<p><a name="identifiers"></a></p>
<h2 id="-">标识符</h2>
<p>标识符（<em>identifiers</em>）可以由以下的字符开始：大写或小写的字母 <code>A</code> 到 <code>Z</code>、下划线 <code>_</code>、基本多语言面（<em>Basic Multilingual Plane</em>）中的 Unicode 非组合字符以及基本多语言面以外的非专用区（<em>Private Use Area</em>）字符。首字符之后，标识符允许使用数字和 Unicode 字符组合。</p>
<p>使用保留字（<em>reserved word</em>）作为标识符，需要在其前后增加反引号 <code>`</code>。例如，<code>class</code> 不是合法的标识符，但可以使用 <code>`class`</code>。反引号不属于标识符的一部分，<code>`x`</code> 和 <code>x</code> 表示同一标识符。</p>
<p>闭包（<em>closure</em>）中如果没有明确指定参数名称，参数将被隐式命名为 <code>$0</code>、<code>$1</code>、<code>$2</code>... 这些命名在闭包作用域内是合法的标识符。</p>
<blockquote>
<p>标识符语法</p>
<p><em>identifier</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">identifier-characters</a>­ <em>opt</em></p>
<p><em>identifier</em> → `­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">identifier-characters­</a> <em>opt­</em> `­</p>
<p><em>identifier</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/implicit-parameter-name" target="_blank">implicit-parameter-name­</a></p>
<p><em>identifier-list</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank">identifier­</a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier" target="_blank">identifier­</a> , <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-list" target="_blank">­identifier-list</a>­</p>
<p><em>identifier-head</em> → A 到 Z 大写或小写字母</p>
<p><em>identifier-head</em> → U+00A8, U+00AA, U+00AD, U+00AF, U+00B2–U+00B5, 或 U+00B7–U+00BA</p>
<p><em>identifier-head</em> → U+00BC–U+00BE, U+00C0–U+00D6, U+00D8–U+00F6, 或 U+00F8–U+00FF</p>
<p><em>identifier-head</em> → U+0100–U+02FF, U+0370–U+167F, U+1681–U+180D, 或 U+180F–U+1DBF</p>
<p><em>identifier-head</em> → U+1E00–U+1FFF</p>
<p><em>identifier-head</em> → U+200B–U+200D, U+202A–U+202E, U+203F–U+2040, U+2054, 或 U+2060–U+206F</p>
<p><em>identifier-head</em> → U+2070–U+20CF, U+2100–U+218F, U+2460–U+24FF, 或 U+2776–U+2793</p>
<p><em>identifier-head</em> → U+2C00–U+2DFF 或 U+2E80–U+2FFF</p>
<p><em>identifier-head</em> → U+3004–U+3007, U+3021–U+302F, U+3031–U+303F, 或 U+3040–U+D7FF</p>
<p><em>identifier-head</em> → U+F900–U+FD3D, U+FD40–U+FDCF, U+FDF0–U+FE1F, 或 U+FE30–U+FE44</p>
<p><em>identifier-head</em> → U+FE47–U+FFFD</p>
<p><em>identifier-head</em> → U+10000–U+1FFFD, U+20000–U+2FFFD, U+30000–U+3FFFD, 或 U+40000–U+4FFFD</p>
<p><em>identifier-head</em> → U+50000–U+5FFFD, U+60000–U+6FFFD, U+70000–U+7FFFD, 或 U+80000–U+8FFFD</p>
<p><em>identifier-head</em> → U+90000–U+9FFFD, U+A0000–U+AFFFD, U+B0000–U+BFFFD, 或 U+C0000–U+CFFFD</p>
<p><em>identifier-head</em> → U+D0000–U+DFFFD 或 U+E0000–U+EFFFD</p>
<p><em>identifier-character</em> → 数字 0 到 9</p>
<p><em>identifier-character</em> → U+0300–U+036F, U+1DC0–U+1DFF, U+20D0–U+20FF, or U+FE20–U+FE2F</p>
<p><em>identifier-character</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-head" target="_blank">identifier-head­</a></p>
<p><em>identifier-characters</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-character" target="_blank">identifier-character</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/identifier-characters" target="_blank">­identifier-characters­</a> <em>opt­</em></p>
<p><em>implicit-parameter-name</em> → <strong>$­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits" target="_blank">decimal-digits­</a></p>
</blockquote>
<p><a name="keywords"></a></p>
<h2 id="-">关键字</h2>
<p>被保留的关键字（<em>keywords</em>）不允许用作标识符，除非被反引号转义，参见 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-XID_796" target="_blank">标识符</a>。</p>
<ul>
<li><p><strong>用作声明的关键字：</strong> <em>class</em>、<em>deinit</em>、<em>enum</em>、<em>extension</em>、<em>func</em>、<em>import</em>、<em>init</em>、<em>let</em>、<em>protocol</em>、<em>static</em>、<em>struct</em>、<em>subscript</em>、<em>typealias</em>、<em>var</em></p>
</li>
<li><p><strong>用作语句的关键字：</strong> <em>break</em>、<em>case</em>、<em>continue</em>、<em>default</em>、<em>do</em>、<em>else</em>、<em>fallthrough</em>、<em>if</em>、<em>in</em>、<em>for</em>、<em>return</em>、<em>switch</em>、<em>where</em>、<em>while</em></p>
</li>
<li><p><strong>用作表达和类型的关键字：</strong> <em>as</em>、<em>dynamicType</em>、<em>is</em>、<em>new</em>、<em>super</em>、<em>self</em>、<em>Self</em>、<em>Type</em>、<em>__COLUMN__</em>、<em>__FILE__</em>、<em>__FUNCTION__</em>、<em>__LINE__</em></p>
</li>
<li><p><strong>特定上下文中被保留的关键字：</strong> <em>associativity</em>、<em>didSet</em>、<em>get</em>、<em>infix</em>、<em>inout</em>、<em>left</em>、<em>mutating</em>、<em>none</em>、<em>nonmutating</em>、<em>operator</em>、<em>override</em>、<em>postfix</em>、<em>precedence</em>、<em>prefix</em>、<em>right</em>、<em>set</em>、<em>unowned</em>、<em>unowned(safe)</em>、<em>unowned(unsafe)</em>、<em>weak</em>、<em>willSet</em>，这些关键字在特定上下文之外可以被用于标识符。</p>
</li>
</ul>
<p><a name="literals"></a></p>
<h2 id="-">字面量</h2>
<p>字面值表示整型、浮点型数字或文本类型的值，举例如下：</p>
<pre><code>42                 // 整型字面量
3.14159            // 浮点型字面量
&quot;Hello, world!&quot;    // 文本型字面量
</code></pre><blockquote>
<p>字面量语法</p>
<p><em>literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/integer-literal" target="_blank">integer-literal­</a> | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-literal" target="_blank">floating-point-literal</a>­ | <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/string-literal" target="_blank">string-literal­</a></p>
</blockquote>
<h3 id="-">整型字面量</h3>
<p>整型字面量（<em>integer literals</em>）表示未指定精度整型数的值。整型字面量默认用十进制表示，可以加前缀来指定其他的进制，二进制字面量加 <code>0b</code>，八进制字面量加 <code>0o</code>，十六进制字面量加 <code>0x</code>。</p>
<p>十进制字面量包含数字 <code>0</code> 至 <code>9</code>。二进制字面量只包含 <code>0</code> 或 <code>1</code>，八进制字面量包含数字 <code>0</code> 至 <code>7</code>，十六进制字面量包含数字 <code>0</code> 至 <code>9</code> 以及字母 <code>A</code> 至 <code>F</code> （大小写均可）。</p>
<p>负整数的字面量在数字前加减号 <code>-</code>，比如 <code>-42</code>。</p>
<p>允许使用下划线 <code>_</code> 来增加数字的可读性，下划线不会影响字面量的值。整型字面量也可以在数字前加 <code>0</code>，同样不会影响字面量的值。</p>
<pre><code>1000_000     // 等于 1000000
005          // 等于 5
</code></pre><p>除非特殊指定，整型字面量的默认类型为 Swift 标准库类型中的 <code>Int</code>。Swift 标准库还定义了其他不同长度以及是否带符号的整数类型，请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_411" target="_blank">整数类型</a>。</p>
<blockquote>
<p>整型字面量语法</p>
<p><em>integer-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal" target="_blank">binary-literal­</a></p>
<p><em>integer-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal" target="_blank">octal-literal­</a></p>
<p><em>integer-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal­</a></p>
<p><em>integer-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal</a>­</p>
<p><em>binary-literal</em> → <strong>0b</strong>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit" target="_blank">binary-digit</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters" target="_blank">binary-literal-characters­</a> <em>opt­</em></p>
<p><em>binary-digit</em> → 数字 0 或 1</p>
<p><em>binary-literal-character</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-digit" target="_blank">binary-digit</a>­ | _­</p>
<p><em>binary-literal-characters</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-character" target="_blank">binary-literal-character</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/binary-literal-characters" target="_blank">binary-literal-characters­</a> <em>opt­</em></p>
<p><em>octal-literal</em> → <strong>0o</strong>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit" target="_blank">octal-digit</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters" target="_blank">octal-literal-characters­</a> <em>opt­</em></p>
<p><em>octal-digit</em> → 数字 0 至 7</p>
<p><em>octal-literal-character</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-digit" target="_blank">octal-digit</a>­ | _­</p>
<p><em>octal-literal-characters</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-character" target="_blank">octal-literal-character</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/octal-literal-characters" target="_blank">­octal-literal-characters­</a> <em>opt­</em></p>
<p><em>decimal-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters" target="_blank">­decimal-literal-characters­</a> <em>opt­</em></p>
<p><em>decimal-digit</em> → 数字 0 至 9</p>
<p><em>decimal-digits</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digits" target="_blank">decimal-digits­</a> <em>opt­</em></p>
<p><em>decimal-literal-character</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-digit" target="_blank">decimal-digit­</a> | _­</p>
<p><em>decimal-literal-characters</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-character" target="_blank">decimal-literal-character</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal-characters" target="_blank">decimal-literal-characters­</a> <em>opt­</em></p>
<p><em>hexadecimal-literal</em> → <strong>0x</strong> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters" target="_blank">­hexadecimal-literal-characters­</a> <em>opt­</em></p>
<p><em>hexadecimal-digit</em> → 数字 0 到 9, a 到 f, 或 A 到 F</p>
<p><em>hexadecimal-literal-character</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a>­ | _­</p>
<p><em>hexadecimal-literal-characters</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-character" target="_blank">hexadecimal-literal-character­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal-characters" target="_blank">hexadecimal-literal-characters</a> ­<em>opt­</em></p>
</blockquote>
<h3 id="-">浮点型字面量</h3>
<p>浮点型字面量（<em>floating-point literals</em>）表示未指定精度浮点数的值。</p>
<p>浮点型字面量默认用十进制表示（无前缀），也可以用十六进制表示（加前缀 <code>0x</code>）。</p>
<p>十进制浮点型字面量（<em>decimal floating-point literals</em>）由十进制数字串后跟小数部分或指数部分（或两者皆有）组成。十进制小数部分由小数点 <code>.</code> 后跟十进制数字串组成。指数部分由大写或小写字母 <code>e</code> 后跟十进制数字串组成，这串数字表示 <code>e</code> 之前的数量乘以 10 的几次方。例如：<code>1.25e2</code> 表示 <code>1.25 ⨉ 10^2</code>，也就是 <code>125.0</code>；同样，<code>1.25e－2</code> 表示 <code>1.25 ⨉ 10^－2</code>，也就是 <code>0.0125</code>。</p>
<p>十六进制浮点型字面量（<em>hexadecimal floating-point literals</em>）由前缀 <code>0x</code> 后跟可选的十六进制小数部分以及十六进制指数部分组成。十六进制小数部分由小数点后跟十六进制数字串组成。指数部分由大写或小写字母 <code>p</code> 后跟十进制数字串组成，这串数字表示 <code>p</code> 之前的数量乘以 2 的几次方。例如：<code>0xFp2</code> 表示 <code>15 ⨉ 2^2</code>，也就是 <code>60</code>；同样，<code>0xFp-2</code> 表示 <code>15 ⨉ 2^-2</code>，也就是 <code>3.75</code>。</p>
<p>与整型字面量不同，负的浮点型字面量由一元运算符减号 <code>-</code> 和浮点型字面量组成，例如 <code>-42.0</code>。这代表一个表达式，而不是一个浮点整型字面量。</p>
<p>允许使用下划线 <code>_</code> 来增强可读性，下划线不会影响字面量的值。浮点型字面量也可以在数字前加 <code>0</code>，同样不会影响字面量的值。</p>
<pre><code>10_000.56     // 等于 10000.56
005000.76     // 等于 5000.76
</code></pre><p>除非特殊指定，浮点型字面量的默认类型为 Swift 标准库类型中的 <code>Double</code>，表示64位浮点数。Swift 标准库也定义 <code>Float</code> 类型，表示32位浮点数。</p>
<blockquote>
<p>浮点型字面量语法</p>
<p><em>floating-point-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-fraction" target="_blank">decimal-fraction­</a> <em>opt</em> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-exponent" target="_blank">decimal-exponent­</a> <em>opt­</em></p>
<p><em>floating-point-literal</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal</a> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-fraction" target="_blank">hexadecimal-fraction</a> ­<em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-exponent" target="_blank">hexadecimal-exponent</a>­</p>
<p><em>decimal-fraction</em> → . <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">­decimal-literal­</a></p>
<p><em>decimal-exponent</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-e" target="_blank">floating-point-e</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign" target="_blank">­sign­</a> <em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/decimal-literal" target="_blank">decimal-literal</a>­</p>
<p><em>hexadecimal-fraction</em> → . <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">­hexadecimal-literal­</a> <em>opt­</em></p>
<p><em>hexadecimal-exponent</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/floating-point-p" target="_blank">floating-point-p­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/sign" target="_blank">sign­</a> <em>opt­</em> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-literal" target="_blank">hexadecimal-literal­</a></p>
<p><em>floating-point-e</em> → <strong>e­</strong> | <strong>E</strong>­</p>
<p><em>floating-point-p</em> → <strong>p</strong>­ | <strong>P</strong>­</p>
<p><em>sign</em> → <strong>+</strong>­ | <strong>-</strong>­</p>
</blockquote>
<h3 id="-">文本型字面量</h3>
<p>文本型字面量（<em>string literal</em>）由双引号中的字符串组成，形式如下：</p>
<pre><code>&quot;characters&quot;
</code></pre><p>文本型字面量中不能包含未转义的双引号 <code>&quot;</code>、未转义的反斜线<code>\</code>、回车符（<em>carriage return</em>）或换行符（<em>line feed</em>）。</p>
<p>可以在文本型字面量中使用的转义特殊符号如下：</p>
<ul>
<li>空字符（Null Character）<code>\0</code></li>
<li>反斜线（Backslash）<code>\\</code></li>
<li>水平 Tab （Horizontal Tab）<code>\t</code></li>
<li>换行符（Line Feed）<code>\n</code></li>
<li>回车符（Carriage Return）<code>\r</code></li>
<li>双引号（Double Quote）<code>\&quot;</code></li>
<li>单引号（Single Quote）<code>\&#39;</code></li>
</ul>
<p>字符也可以用以下方式表示：</p>
<ul>
<li><code>\x</code> 后跟两位十六进制数字</li>
<li><code>\u</code> 后跟四位十六进制数字</li>
<li><code>\U</code> 后跟八位十六进制数字</li>
</ul>
<p>后跟的数字表示一个 Unicode 码点。</p>
<p>文本型字面量允许在反斜线小括号 <code>\()</code> 中插入表达式的值。插入表达式（<em>interpolated expression</em>）不能包含未转义的双引号 <code>&quot;</code>、反斜线 <code>\</code>、回车符或者换行符。表达式值的类型必须在 <em>String</em> 类中有对应的初始化方法。</p>
<p>例如，以下所有文本型字面量的值相同：</p>
<pre><code>&quot;1 2 3&quot;
&quot;1 2 \(3)&quot;
&quot;1 2 \(1 + 2)&quot;
var x = 3; &quot;1 2 \(x)&quot;
</code></pre><p>文本型字面量的默认类型为 <code>String</code>。组成字符串的字符类型为 <code>Character</code>。更多有关 <code>String</code> 和 <code>Character</code> 的信息请参照 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-XID_368" target="_blank">字符串和字符</a>。</p>
<blockquote>
<p>文本型字面量语法</p>
<p><em>string-literal</em> → <strong>&quot;­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text" target="_blank">quoted-text</a> <strong>­&quot;­</strong></p>
<p><em>quoted-text</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text-item" target="_blank">quoted-text-item­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/quoted-text" target="_blank">quoted-text</a> ­<em>opt­</em></p>
<p><em>quoted-text-item</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/escaped-character" target="_blank">escaped-character­</a></p>
<p><em>quoted-text-item</em> → <strong>(</strong>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/swift/grammar/expression" target="_blank">expression­</a> <strong>)</strong>­</p>
<p><em>quoted-text-item</em> → 除 <code>&quot;</code>、<code>\</code>­、<code>U+000A</code> 或 <code>U+000D</code> 以外的任何 Unicode 扩展字符集</p>
<p><em>escaped-character</em> → <strong>\0­</strong> | <strong>\</strong>­ | <strong>\t­</strong> | <strong>\n</strong>­ | <strong>\r­</strong> | <strong>\&quot;­</strong> | <strong>\&#39;</strong>­</p>
<p><em>escaped-character</em> → <strong>\x­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
<p><em>escaped-character</em> → <strong>\u</strong> ­<a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">­hexadecimal-digit</a>­ <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
<p><em>escaped-character</em> → <strong>\U­</strong> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/hexadecimal-digit" target="_blank">hexadecimal-digit­</a></p>
</blockquote>
<p><a name="operators"></a></p>
<h2 id="-">运算符</h2>
<p>Swift 标准库定义了许多可供使用的运算符，其中大部分在 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-XID_70" target="_blank">基础运算符</a> 和 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_28" target="_blank">高级运算符</a> 中进行了阐述。这里将描述哪些字符能用作运算符。</p>
<p>运算符由一个或多个以下字符组成：
<code>/</code>、<code>=</code>、<code>-</code>、<code>+</code>、<code>!</code>、<code>*</code>、<code>%</code>、<code>&lt;</code>、<code>&gt;</code>、<code>&amp;</code>、<code>|</code>、<code>^</code>、<code>~</code>、<code>.</code>。也就是说，标记 <code>=</code>, <code>-&gt;</code>、<code>//</code>、<code>/*</code>、<code>*/</code>、<code>.</code> 以及一元前缀运算符 <code>&amp;</code> 属于保留字，这些标记不能被重写或用于自定义运算符。</p>
<p>运算符两侧的空白被用来区分该运算符是否为前缀运算符（<em>prefix operator</em>）、后缀运算符（<em>postfix operator</em>）或二元运算符（<em>binary operator</em>）。规则总结如下：</p>
<ul>
<li><p>如果运算符两侧都有空白或两侧都无空白，将被看作二元运算符。例如：<code>a+b</code> 和 <code>a + b</code> 中的运算符 <code>+</code> 被看作二元运算符。</p>
</li>
<li><p>如果运算符只有左侧空白，将被看作前缀一元运算符。例如 <code>a ++b</code> 中的 <code>++</code> 被看作前缀一元运算符。</p>
</li>
<li><p>如果运算符只有右侧空白，将被看作后缀一元运算符。例如 <code>a++ b</code> 中的 <code>++</code> 被看作后缀一元运算符。</p>
</li>
<li><p>如果运算符左侧没有空白并紧跟 <code>.</code>，将被看作后缀一元运算符。例如 <code>a++.b</code> 中的 <code>++</code> 被看作后缀一元运算符（同理， <code>a++ . b</code> 中的 <code>++</code> 是后缀一元运算符而 <code>a ++ .b</code> 中的 <code>++</code> 不是）.</p>
</li>
</ul>
<p>鉴于这些规则，运算符前的字符 <code>(</code>、<code>[</code> 和 <code>{</code> ；运算符后的字符 <code>)</code>、<code>]</code> 和 <code>}</code> 以及字符 <code>,</code>、<code>;</code> 和 <code>:</code> 都将用于空白检测。</p>
<p>以上规则需注意一点，如果运算符 <code>!</code> 或 <code>?</code> 左侧没有空白，则不管右侧是否有空白都将被看作后缀运算符。如果将 <code>?</code> 用作可选类型（<em>optional type</em>）修饰，左侧必须无空白。如果用于条件运算符 <code>? :</code>，必须两侧都有空白。</p>
<p>在特定构成中 ，以 <code>&lt;</code> 或 <code>&gt;</code> 开头的运算符会被分离成两个或多个标记，剩余部分以同样的方式会被再次分离。因此，在 <code>Dictionary&lt;String, Array&lt;Int&gt;&gt;</code> 中没有必要添加空白来消除闭合字符 <code>&gt;</code> 的歧义。在这个例子中， 闭合字符 <code>&gt;</code> 被看作单字符标记，而不会被误解为移位运算符 <code>&gt;&gt;</code>。</p>
<p>要学习如何自定义新的运算符，请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_48" target="_blank">自定义操作符</a> 和 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-XID_644" target="_blank">运算符声明</a>。学习如何重写现有运算符，请参考 <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html#//apple_ref/doc/uid/TP40014097-CH27-XID_43" target="_blank">运算符方法</a>。</p>
<blockquote>
<p>运算符语法</p>
<p><em>operator</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator-character" target="_blank">operator-character­</a> <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a> <em>opt­</em></p>
<p><em>operator-character</em> → <strong>/­</strong> | <strong>=­</strong> | <strong>-­</strong> | <strong>+­</strong> | <strong>!­</strong> | <strong>*­</strong> | <strong>%­</strong> | <strong>&lt;­</strong> | <strong>&gt;­</strong> | <strong>&amp;­</strong> | <strong>|­</strong> | <strong>^­</strong> | <strong>~­</strong> | <strong>.­</strong></p>
<p><em>binary-operator</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
<p><em>prefix-operator</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
<p><em>postfix-operator</em> → <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/swift/grammar/operator" target="_blank">operator­</a></p>
</blockquote>

                    
                    </section>
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3/01_About_the_Language_Reference.html" class="navigation navigation-prev " aria-label="Previous page: 关于语言参考"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/03_Types.html" class="navigation navigation-next " aria-label="Next page: 类型"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        

        <script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
        <script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
        <script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
        <script src="../gitbook/app.js"></script>
        

    
    <script src="../gitbook/plugins/gitbook-plugin-mixpanel/plugin.js"></script>
    

    
    <script src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
